/*
 * Copyright (c) 2017 jinbei6.com All Rights Reserved.
 */
package com.changjiang.luban.utils;

import lombok.extern.java.Log;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.util.CollectionUtils;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述:数据转换工具类
 * @author nichangjiang@vip.qq.com
 * 创建时间:2017/12/4. 11:30
 * 修改人:
 * 修改时间:
 * 修改备注:
 * 版本:v1.0
 */
@Log
public class ConvertUtil {

    /** 下划线 */
    private static final String UNDERLINE="_";

    /**
     * 数据格式转换
     * @param clazz  转换的结果实体
     * @param map 数据
     * @return
     * @throws Exception
     */
    public static Object convertObject(Class<?> clazz,Map<String, Object> map) {
        PropertyDescriptor[] props = new PropertyDescriptor[0];
        try {
            props = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        Object obj = null;
        try {
            obj = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        for (String key:map.keySet()) {
            String attrName = key;
            for (PropertyDescriptor prop : props) {
                attrName = removeUnderLine(attrName);
                if (!attrName.equals(prop.getName())) {
                    continue;
                }
                Method method = prop.getWriteMethod();
                Object value = map.get(key);
                if (value != null) {
                    value = ConvertUtils.convert(value,prop.getPropertyType());
                }
                try {
                    method.invoke(obj,value);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info(value+"-----------转换成为");
                }
            }
        }
        return obj;
    }

    /**
     * 数据格式转换(集合)
     * @param clazz 转换的结果实体
     * @param list 需要转换的数据
     * @return
     */
    public static List<? extends Object> convertList(Class<?> clazz, List<Map<String, Object>> list) {
        List<Object> result;
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        result = new ArrayList<>();
        try {
            for (Map<String, Object> map : list) {
                Object obj = convertObject(clazz,map);
                result.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("数据转换错误");
        }
        return result;
    }

    /**
     * 去掉数据库字段的下划线
     * @param attrName 属性字段名称
     * @return
     */
    public static String removeUnderLine(String attrName) {
        if(attrName.contains(UNDERLINE)) {
            String[] names = attrName.split("_");
            String firstPart = names[0];
            String otherPart = "";
            for (int i = 1; i < names.length; i++) {
                String word = names[i].replaceFirst(names[i].substring(0, 1), names[i].substring(0, 1).toUpperCase());
                otherPart += word;
            }
            attrName = firstPart + otherPart;
        }
        return attrName;
    }

}
